﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Meqs.Entity;
using Meqs.Logic.Interface;

public partial class Education_SchoolArea : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    //初始化 repeat控件，集成显示教师关联信息
    protected void btnBind_Click(object sender, EventArgs e)
    {
        Bind(1);
    }

    private void Bind(int pageIndex)
    {
        //获取查询条件类型和值
        int qType = int.Parse(HFQueryType.Value);
        string qValue = HFQueryValue.Value;

        ISchoolAreaLogic schoolAreaLogic = Container<ISchoolAreaLogic>.Instance;
        List<SchoolArea> list = null;
        Hashtable table = new Hashtable();

        switch (qType)
        {
            //0: 查询所有
            case 0:
                //根据条件查询数据库
                list = (List<SchoolArea>)schoolAreaLogic.FindAll();

                //分页处理
                PagerBind(list, pageIndex);
                
                break;
            //1: 按学区编号查询
            case 1:
                //根据条件查询数据库
                table.Add("SchoolAreaMark", qValue);
                list = (List<SchoolArea>)schoolAreaLogic.FindAll(table);

                //分页处理
                PagerBind(list, pageIndex);

                break;
            //2: 按学区名称查询
            case 2:
                //根据条件查询数据库
                table.Add("SchoolAreaName", qValue);
                list = (List<SchoolArea>)schoolAreaLogic.FindAll(table);

                //分页处理
                PagerBind(list, pageIndex);

                break;
            default: 
                break;
        }
    }

    //分页处理
    private void PagerBind(List<SchoolArea> srcList, int pageIndex)
    {
        AspNetPager1.RecordCount = srcList.Count;
        AspNetPager1.CurrentPageIndex = pageIndex;

        int index = (pageIndex - 1) * AspNetPager1.PageSize;
        int count = 0;
        if (srcList.Count - index >= AspNetPager1.PageSize) count = AspNetPager1.PageSize;
        else count = srcList.Count - index;

        List<SchoolArea> pageList = srcList.GetRange(index, count);

        Repeater1.DataSource = pageList;
        Repeater1.DataBind();
    }

    protected void AspNetPager1_PageChanged(object src, EventArgs e)
    {
        Wuqi.Webdiyer.PageChangedEventArgs pc = e as Wuqi.Webdiyer.PageChangedEventArgs;

        Bind(pc.NewPageIndex);
    }

    /// <summary>
    /// 添加学区信息
    /// </summary>
    /// <param name="schoolArea"></param>
    /// <returns></returns>
    [System.Web.Services.WebMethod]
    public static int InsertSchoolAreaInfo(SchoolArea schoolArea)
    {
        int areaId = Container<ISchoolAreaLogic>.Instance.Create(schoolArea);
        return areaId;
    }

    /// <summary>
    /// 修改学区信息
    /// </summary>
    /// <param name="schoolArea"></param>
    /// <returns></returns>
    [System.Web.Services.WebMethod]
    public static int UpdateSchoolAreaInfo(SchoolArea schoolArea)
    {
        int n = Container<ISchoolAreaLogic>.Instance.Update(schoolArea);
        return n;
    }

    /// <summary>
    /// 删除学区信息
    /// </summary>
    /// <param name="areaId"></param>
    /// <returns></returns>
    [System.Web.Services.WebMethod]
    public static int DeleteSchoolArea(int areaId)
    {
        int n = Container<ISchoolAreaLogic>.Instance.Delete(areaId);
        return n;
    }

    //查询学区信息（根据ID）
    [System.Web.Services.WebMethod]
    public static SchoolArea SelectSohoolArea(int areaId)
    {
        ISchoolAreaLogic schoolAreaLogic = Container<ISchoolAreaLogic>.Instance;
        SchoolArea sa = schoolAreaLogic.FindById(areaId);
        return sa;
    }

    //验证学区编号和学区名字，不能重复
    [System.Web.Services.WebMethod]
    public static int ValidateAreaInfo(SchoolArea schoolArea)
    {
        int result = 0;
        ISchoolAreaLogic schoolAreaLogic = Container<ISchoolAreaLogic>.Instance;

        //1.新增
        if (schoolArea.RID == 0)
        {
            Hashtable tableMark = new Hashtable();
            tableMark.Add("SchoolAreaMark", schoolArea.SchoolAreaMark);
            result = schoolAreaLogic.FindCount(tableMark);
            //学区编号重复
            if (result > 0) return 1;

            Hashtable tableName = new Hashtable();
            tableName.Add("SchoolAreaName", schoolArea.SchoolAreaName);
            result = schoolAreaLogic.FindCount(tableName);
            //学区名称重复
            if (result > 0) return 2;
        }
        //2.修改
        else
        {
            SchoolArea sa = schoolAreaLogic.FindById(schoolArea.RID);
            //修改了学区编号
            if (sa.SchoolAreaMark.ToUpper() != schoolArea.SchoolAreaMark.ToUpper())
            {
                Hashtable tableMark = new Hashtable();
                tableMark.Add("SchoolAreaMark", schoolArea.SchoolAreaMark);
                result = schoolAreaLogic.FindCount(tableMark);
                //学区编号重复
                if (result > 0) return 1;
            }

            //修改了学区名称
            if (sa.SchoolAreaName.ToUpper() != schoolArea.SchoolAreaName.ToUpper())
            {
                Hashtable tableName = new Hashtable();
                tableName.Add("SchoolAreaName", schoolArea.SchoolAreaName);
                result = schoolAreaLogic.FindCount(tableName);
                //学区名称重复
                if (result > 0) return 2;
            }
        }

        return result;
    }

    //验证学区能否删除
    [System.Web.Services.WebMethod]
    public static int ValidateDelete(int areaId)
    {
        int result = 0;
        ISchoolInfoLogic schoolInfoLogic = Container<ISchoolInfoLogic>.Instance;

        Hashtable ht = new Hashtable();
        ht.Add("AreaID", areaId);
        ht.Add("IsEffect", true);
        result = schoolInfoLogic.FindCount(ht);
        //该学区存在学校
        if (result > 0) return 1;

        return result;
    }
}
